using UnityEngine;
using System.Collections.Generic;
using System;

namespace UndergroundSurvival.Managers
{
    public class ResourceManager : MonoBehaviour
    {
        public static ResourceManager Instance { get; private set; }

        [System.Serializable]
        public class Resource
        {
            public string name;
            public float amount;
            public float maxAmount;
        }

        [Header("Resources")]
        [SerializeField] private List<Resource> resources = new List<Resource>();

        public event Action<string, float> OnResourceChanged;

        private void Awake()
        {
            if (Instance == null)
            {
                Instance = this;
                DontDestroyOnLoad(gameObject);
            }
            else
            {
                Destroy(gameObject);
            }
        }

        private void Start()
        {
            InitializeResources();
        }

        private void InitializeResources()
        {
            // 初始化基础资源
            AddResource("Wood", 100f, 1000f);
            AddResource("Metal", 50f, 500f);
            AddResource("Food", 50f, 200f);
            AddResource("Water", 100f, 300f);
            AddResource("Power", 100f, 200f);
        }

        public void AddResource(string name, float amount, float maxAmount)
        {
            Resource resource = resources.Find(r => r.name == name);
            if (resource == null)
            {
                resource = new Resource { name = name, amount = amount, maxAmount = maxAmount };
                resources.Add(resource);
            }
            else
            {
                resource.amount = amount;
                resource.maxAmount = maxAmount;
            }
            OnResourceChanged?.Invoke(name, resource.amount);
        }

        public bool HasEnoughResource(string name, float amount)
        {
            Resource resource = resources.Find(r => r.name == name);
            return resource != null && resource.amount >= amount;
        }

        public bool ConsumeResource(string name, float amount)
        {
            Resource resource = resources.Find(r => r.name == name);
            if (resource != null && resource.amount >= amount)
            {
                resource.amount -= amount;
                OnResourceChanged?.Invoke(name, resource.amount);
                return true;
            }
            return false;
        }

        public bool AddResourceAmount(string name, float amount)
        {
            Resource resource = resources.Find(r => r.name == name);
            if (resource != null)
            {
                resource.amount = Mathf.Min(resource.amount + amount, resource.maxAmount);
                OnResourceChanged?.Invoke(name, resource.amount);
                return true;
            }
            return false;
        }

        public float GetResourceAmount(string name)
        {
            Resource resource = resources.Find(r => r.name == name);
            return resource?.amount ?? 0f;
        }

        public float GetResourceMaxAmount(string name)
        {
            Resource resource = resources.Find(r => r.name == name);
            return resource?.maxAmount ?? 0f;
        }

        public bool ConsumeResources(List<Rooms.ResourceCost> costs)
        {
            // 首先检查是否有足够的资源
            foreach (var cost in costs)
            {
                if (!HasEnoughResource(cost.resourceName, cost.amount))
                {
                    return false;
                }
            }

            // 消耗资源
            foreach (var cost in costs)
            {
                ConsumeResource(cost.resourceName, cost.amount);
            }

            return true;
        }

        public void UpdateResourceMaxAmount(string name, float newMaxAmount)
        {
            Resource resource = resources.Find(r => r.name == name);
            if (resource != null)
            {
                resource.maxAmount = newMaxAmount;
                resource.amount = Mathf.Min(resource.amount, newMaxAmount);
                OnResourceChanged?.Invoke(name, resource.amount);
            }
        }
    }
} 